package com.jwsoft.manager.core.task.sls;

import com.alibaba.fastjson.JSONObject;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.xxl.job.core.handler.annotation.XxlJob;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;


import java.io.*;


/**
 * Created with CIB Co.
 *
 * @author ASUS
 * @createTime 2023-11-17 16:06
 * @update: [序号][日期YYYY-MM-DD] [更改人姓名][变更描述]
 * @since 1.0
 */
@Component
public class SlslogJob {
    private static Logger logger = LoggerFactory.getLogger(SlslogJob.class);

    @Autowired
    private CommonIntegration commonIntegration;

    @XxlJob("slslogJobHandler")
    public void slslogJobHandler() throws Exception{
        //获取本地保存的文件路径
        CommonVO commonVo = new CommonVO();
        commonVo.setParamKey("sls_local_file_path");
        String slsLocalFilePath = commonIntegration.getValueByKey(commonVo);
        //获取远端需要保存的文件路径
        commonVo.setParamKey("sls_cloud_file_path");
        String slsCloudFilePath = commonIntegration.getValueByKey(commonVo);
        //获取需要上传的服务器地址
        commonVo.setParamKey("sls_cloud_file_url");
        String slsCloudFileUrl = commonIntegration.getValueByKey(commonVo);
        this.fileUpLoad(slsLocalFilePath,slsCloudFilePath,slsCloudFileUrl);
    }

    //创建测试方法
    public static void main(String[] args) throws Exception {
        SlslogJob slslogJob = new SlslogJob();
        slslogJob.fileUpLoad("F:\\MyCode\\git-xsbm\\logs\\xsbm\\log_sls.log","E:\\log\\log_sls.log","http://localhost:9020/logApi/api/log/file/upload");
    }

    private void fileUpLoad(String slsLocalFilePath,String slsCloudFilePath,String urlStr) throws Exception{

        if(
                slsLocalFilePath!= null &&
                        !"".equals(slsLocalFilePath) &&
                        !"null".equals(slsLocalFilePath)
        ){

            if(
                    slsCloudFilePath!= null &&
                            !"".equals(slsCloudFilePath) &&
                            !"null".equals(slsCloudFilePath)
            ){
                File file = new File(slsLocalFilePath);
                try {
    		        RestTemplate rest = new RestTemplate();
                    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
                    //接口参数
                    FileSystemResource resource = new FileSystemResource(file);
                    map.add("file", resource);
                    map.add("path", slsCloudFilePath);
                    //头部类型
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
                    //构造实体对象
                    HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
                    //发起请求,服务地址，请求参数，返回消息体的数据类型
                    JSONObject rst = rest.postForEntity(urlStr, param, JSONObject.class).getBody();
                    if(rst. getInteger("code") == 0){
                        logger.info("文件上传成功");
                    }else {
                        throw new AppException("sls日志上传失败" );
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new AppException("sls日志上传失败:"+e.getMessage() );
                }
            }else{
                throw new AppException("远端文件路径不正确！" );
            }
        }else{
            throw new AppException("文件获取失败！" );
        }
    }


}
